package com.atguigu.flink.chapter05.transform;

import com.atguigu.flink.bean.WaterSensor;
import com.atguigu.flink.chapter05.function.WaterSensorMapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
/*
聚合算子;
    1、必须先KeyBy, 再聚合
    2、sum(position) 元组聚合
       sum(field)  pojo类型的某个属性进行聚合
    3、非分组和聚合字段，取得是第一个值

----
sum
max min
maxBy minBy
    随着最大或最小取其他值

    true:当出现相同最大或最小的时候，是否取第一个

reduce


----
select
    id,
    sum(vc)
from t
group by id;
 */
public class SumDemo {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        //不同sensor的水位和
        env.readTextFile("C:\\IDEA\\code\\LearnDemo\\flink\\input\\sensor.txt")
                .map(new WaterSensorMapFunction())
                .keyBy(new KeySelector<WaterSensor, String>() {
                    @Override
                    public String getKey(WaterSensor ws) throws Exception {
                        return ws.getId();
                    }
                })
                //.sum("vc")
                //.max("vc")
                //.min("vc")
                .maxBy("vc",false)  //随着最大值取对应的那个ts
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


//public class SumDemo {
//    public static void main(String[] args) {
//        Configuration conf = new Configuration();
//        conf.setInteger("rest.port",2000);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
//        env.setParallelism(1);
//
//        env.readTextFile("C:\\IDEA\\code\\LearnDemo\\flink\\input\\sensor.txt")
//                .map(new WaterSensorMapFunction())
//                .keyBy(new KeySelector<WaterSensor, String>() {
//                    @Override
//                    public String getKey(WaterSensor ws) throws Exception {
//                        return ws.getId();
//                    }
//                })
//                //.sum("vc")
//                //.max("vc")
//                //.min("vc")
//                .maxBy("vc",false)
//                .print();
//        try {
//            env.execute();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}